package com.opages.netty.demo.chat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @author daizhong.liu
 * @create 2021-12-03 13:55
 **/
public class GroupClient {
    private SocketChannel socketChannel;
    private Selector selector;
    private String username;

    public GroupClient(String address,int port) {
        try {
            socketChannel = SocketChannel.open(new InetSocketAddress(address,port));
            selector = Selector.open();
            socketChannel.configureBlocking(false);
            socketChannel.register(selector, SelectionKey.OP_READ);
            username = socketChannel.getLocalAddress().toString().substring(1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void writeContent(String content) {
        try {
            socketChannel.write(ByteBuffer.wrap((username+" 说:"+content).getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String readContent() {
        try {
            int select = selector.select();
            if (select > 0) {
                Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();
                while (keyIterator.hasNext()) {
                    SelectionKey selectionKey = keyIterator.next();
                    if (selectionKey.isReadable()) {
                        SocketChannel channel = (SocketChannel) selectionKey.channel();
                        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                        int read = channel.read(byteBuffer);
                        if (read > 0) {
                            String content = new String(byteBuffer.array());
                            return content;
                        }
                    }
                    keyIterator.remove();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        final GroupClient client = new GroupClient("127.0.0.1",8889);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        String content = client.readContent();
                        if(content != null && !"".equals(content)) {
                            System.err.println(content);
                        }
                        Thread.sleep(3000);
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String content = scanner.nextLine();
            client.writeContent(content);
        }

    }


}












